﻿Ext.define('app.prm.inquiry.InquiryController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.inquiry',
    init: function () {
        var me = this;
    },
    onAdd: function () {
        var grid = this.getView();
        var win = Ext.create("app.prm.inquiry.Create", { title: '询盘创建' });
        win.on("savesuccess", function (obj) {
            win.close();
            grid.store.load();
        });
        win.show();
    },
    onCellClick: function (table, td, cellIndex, record, tr, rowIndex, e, eOpts) {
        var t = e.position.column.dataIndex;
        if (t == 'projectNo') {
            Ext.util.Cookies.set('projectinfo' + record.data.projectId, record.data.projectName + "(" + record.data.projectNo + ")");

            this.redirectTo('app.prm.detail.Index/' + record.data.projectId);
        }
        else if (t == 'productNo') {
            if (record.data.productId != 0) {
                this.redirectTo('app.prm.product.Detail/' + record.data.productId);
            }
        }
    },
    onDocCelldblclick: function (table, td, cellIndex, record, tr, rowIndex, e, eOpts) {
        var column = e.position.column;
        var value = record.data[column.dataIndex];

        if (value == undefined || value.indexOf('/') == -1) {
            return false;
        }
        var v = value.split('/')[0];
        if (v == 0) {
            return false;
        }

        var DocTypeId = record.data.DocTypeId;
        var projectId = 0;
        var projectName = '';
        var projectNo = '';
        var docTypeId = 0;

        if (DocTypeId >= 0) {
            if (DocTypeId == 0) {
                return false;
            }
            projectId = column.projectId;
            projectName = column.text;
            projectNo = column.projectNo;
            docTypeId = DocTypeId;
        }
        else {
            projectId = record.data.projectId;
            projectName = record.data.projectName;
            projectNo = record.data.projectNo;
            docTypeId = column.docType;
        }
        Ext.util.Cookies.set('projectdocument' + projectId, projectName + "(" + projectNo + ")文档");
        Ext.util.Cookies.set('projectdocumentreport' + projectId, docTypeId);
        this.redirectTo('app.prm.document.Index/' + projectId);
    },
    onEditProject: function (grid, rowIndex, colIndex) {
        var record = grid.store.getAt(rowIndex);
        var win = Ext.create("app.prm.inquiry.Create",
            {
                title: '询盘编辑',
                inquiryId: record.get('inquiryId'),
                subId: record.get('subId')
            });
        win.on("savesuccess", function (obj) {
            win.close();
            grid.store.load();
        });
        win.show();
    },
    onDbClick: function (grid, record, tr, rowIndex, e, ept) {
        this.onEditProject(grid, rowIndex, 0);
    },
    onTAEditProject: function (grid, rowIndex, colIndex) {
        var record = grid.store.getAt(rowIndex);
        var win = Ext.create("app.prm.inquiry.EditTA",
            {
                title: '技术评估',
                inquiryId: record.get('inquiryId'),
                subId: record.get('subId')
            });
        win.on("savesuccess", function (obj) {
            win.close();
            grid.store.load();
        });
        win.show();
    },
    onTADbClick: function (grid, record, tr, rowIndex, e, ept) {
        this.onTAEditProject(grid, rowIndex, 0);
    },
    onInit: function (view) {
        App.AjaxPost('/api/services/app/permission/HasAddProject', {}, function (data) {
            view.getViewModel().set('hasAdd', !data.result);
        });
    },
    onStartSearch: function () {
        var grid = this.getView(),
        panel = grid.lookupReference("inquirysearch");
        grid.store.filter = panel.form.getForm().getValues();
        grid.pagingBar.moveFirst();
    },
    onClearSearch: function () {
        var grid = this.getView(),
        panel = grid.lookupReference("inquirysearch");
        panel.form.getForm().reset();
        this.onStartSearch();
    }
});
